package com.hyd.project;

import java.util.ArrayList;

import com.hyd.adapter.MyFragmentPagerAdapter;
import com.hyd.application.MyApplication;
import com.hyd.fragment.ActionFragment;
import com.hyd.fragment.PictureBookFragment;
import com.hyd.fragment.TalkFragment;
import com.hyd.project.R;
import com.hyd.service.MusicService;
import com.hyd.util.LoginUtil;
import com.hyd.view.RippleView;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.DrawerLayout.DrawerListener;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 首页
 * 
 * @author llf
 * 
 */
public class MainActivity extends FragmentActivity implements
		OnPageChangeListener {
	private long exitTime = 0;
	private TextView picture_book, talk, action;
	private ImageView moving, play, head_portrait, loop;
	private RippleView playRipple, loopRipple, nextRipple, priorRipple;
	private DrawerLayout draw;
	private RelativeLayout personal, player_view;
	private ViewPager viewPage;
	private MyFragmentPagerAdapter adapter;
	private ArrayList<Fragment> fragmentList = new ArrayList<Fragment>();
	private Fragment pictureBookFragment, talkFragment, actionFragment;
	private RelativeLayout person, order, upload, about_us;
	private boolean isLogin;
	private TextView name, sex, age, address;
	private ProgressBar progress;
	private Handler handler, serviceHandler;
	private Runnable runnable;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initView();
		initData();
		initClick();
	}

	private void initView() {
		handler = new Handler();
		runnable = new Runnable() {

			@Override
			public void run() {
				if (MyApplication.playstatus == 1) {
					progress.setMax(MusicService.player.getDuration());
					progress.setProgress(MusicService.player
							.getCurrentPosition());
				}
				// 延迟启动线程，主要是为了保证启动线程的时候,播放音乐的服务已经启动
				handler.postDelayed(runnable, 50);
			}
		};

		picture_book = (TextView) findViewById(R.id.picture);
		talk = (TextView) findViewById(R.id.talk);
		action = (TextView) findViewById(R.id.action);
		personal = (RelativeLayout) findViewById(R.id.rl_personal);
		moving = (ImageView) findViewById(R.id.moving);
		draw = (DrawerLayout) findViewById(R.id.drawer);
		viewPage = (ViewPager) findViewById(R.id.content);
		person = (RelativeLayout) findViewById(R.id.person);
		order = (RelativeLayout) person.findViewById(R.id.my_order);
		upload = (RelativeLayout) person.findViewById(R.id.my_upload);
		about_us = (RelativeLayout) person.findViewById(R.id.about_us);
		head_portrait = (ImageView) person.findViewById(R.id.head_portrait);
		name = (TextView) person.findViewById(R.id.name);
		sex = (TextView) person.findViewById(R.id.sex);
		age = (TextView) person.findViewById(R.id.age);
		address = (TextView) person.findViewById(R.id.address);
		player_view = (RelativeLayout) findViewById(R.id.player_view);
		play = (ImageView) player_view.findViewById(R.id.play);
		loop = (ImageView) player_view.findViewById(R.id.loop);
		playRipple = (RippleView) player_view.findViewById(R.id.play_ripple);
		loopRipple = (RippleView) player_view.findViewById(R.id.loop_ripple);
		nextRipple = (RippleView) player_view.findViewById(R.id.next_ripple);
		priorRipple = (RippleView) player_view.findViewById(R.id.prior_ripple);
		progress = (ProgressBar) player_view.findViewById(R.id.seek);
		viewPage.setCurrentItem(0);

		Intent intent = new Intent(MainActivity.this,
				MusicService.class);
		startService(intent);
	}

	private void initData() {
		isLogin = LoginUtil.isLogin(MainActivity.this);
		if (isLogin == true) {
			// 已经登录了，把名字等信息加上
			// TODO
		} else {
			name.setText("点击登录");
			sex.setVisibility(View.GONE);
			age.setVisibility(View.GONE);
			address.setVisibility(View.GONE);
		}

		pictureBookFragment = new PictureBookFragment();
		talkFragment = new TalkFragment();
		actionFragment = new ActionFragment();
		fragmentList.add(pictureBookFragment);
		fragmentList.add(talkFragment);
		fragmentList.add(actionFragment);
		adapter = new MyFragmentPagerAdapter(getSupportFragmentManager(),
				fragmentList);
		viewPage.setAdapter(adapter);
		viewPage.setOnPageChangeListener(this);
	}

	private void initClick() {
		picture_book.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 不滑动，直接跳到某一页
				viewPage.setCurrentItem(0, false);
			}
		});

		talk.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				viewPage.setCurrentItem(1, false);
			}
		});

		action.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				viewPage.setCurrentItem(2, false);
			}
		});

		personal.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				draw.openDrawer(Gravity.LEFT);
			}
		});

		moving.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this,
						DynamicActivity.class);
				startActivity(intent);
				overridePendingTransition(R.anim.slide_in_right,
						R.anim.slide_out_left);
			}
		});

		draw.setDrawerListener(new DrawerListener() {

			@Override
			public void onDrawerStateChanged(int arg0) {

			}

			@Override
			public void onDrawerSlide(View arg0, float range) {
				personal.setAlpha(1 - range);
			}

			@Override
			public void onDrawerOpened(View arg0) {

			}

			@Override
			public void onDrawerClosed(View arg0) {

			}
		});
		order.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (LoginUtil.isLogin(MainActivity.this) == true) {
					Intent intent = new Intent(MainActivity.this,
							MyOrderActivity.class);
					startActivity(intent);
					overridePendingTransition(R.anim.slide_in_right,
							R.anim.slide_out_left);
				} else {
					Intent intent = new Intent(MainActivity.this,
							LoginActivity.class);
					startActivity(intent);
					overridePendingTransition(R.anim.slide_in_right,
							R.anim.slide_out_left);
				}
			}
		});

		upload.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (LoginUtil.isLogin(MainActivity.this) == true) {
					Intent intent = new Intent(MainActivity.this,
							MyUploadActivity.class);
					startActivity(intent);
					overridePendingTransition(R.anim.slide_in_right,
							R.anim.slide_out_left);
				} else {
					Intent intent = new Intent(MainActivity.this,
							LoginActivity.class);
					startActivity(intent);
					overridePendingTransition(R.anim.slide_in_right,
							R.anim.slide_out_left);
				}
			}
		});

		about_us.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (LoginUtil.isLogin(MainActivity.this) == true) {
					Intent intent = new Intent(MainActivity.this,
							AboutUsActivity.class);
					startActivity(intent);
					overridePendingTransition(R.anim.slide_in_right,
							R.anim.slide_out_left);
				} else {
					Intent intent = new Intent(MainActivity.this,
							LoginActivity.class);
					startActivity(intent);
					overridePendingTransition(R.anim.slide_in_right,
							R.anim.slide_out_left);
				}
			}
		});

		playRipple.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (MyApplication.playstatus == 0) {
					play.setBackgroundResource(R.drawable.player_hesitation);
					MyApplication.playstatus = 1;
					MusicService.goon();
					handler.post(runnable);
				} else {
					MusicService.pause();
					MyApplication.playstatus = 0;
					play.setBackgroundResource(R.drawable.play);
				}
			}
		});
		// 设置为循环播放
		loopRipple.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (MyApplication.isLoop) {
					loop.setBackgroundResource(R.drawable.unloop);
					MusicService.player.setLooping(false);
					MyApplication.isLoop = false;
				} else {
					loop.setBackgroundResource(R.drawable.loop);
					MusicService.player.setLooping(true);
					MyApplication.isLoop = true;
				}
			}
		});

		nextRipple.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				play.setBackgroundResource(R.drawable.player_hesitation);
				MyApplication.playstatus = 1;
				handler.post(runnable);
				MusicService.playNext();
			}
		});

		priorRipple.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				play.setBackgroundResource(R.drawable.player_hesitation);
				MyApplication.playstatus = 1;
				handler.post(runnable);
				MusicService.playPrior();
			}
		});

		head_portrait.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (LoginUtil.isLogin(MainActivity.this) == false) {
					Intent intent = new Intent(MainActivity.this,
							LoginActivity.class);
					startActivity(intent);
					overridePendingTransition(R.anim.slide_in_right,
							R.anim.slide_out_left);
				} else {
					Intent intent = new Intent(MainActivity.this,
							PersonalInfoActivity.class);
					startActivity(intent);
					overridePendingTransition(R.anim.slide_in_right,
							R.anim.slide_out_left);
				}
			}
		});
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {

	}

	@Override
	public void onPageSelected(int position) {
		switch (position) {
		case 0:
			picture_book.setTextColor(getResources().getColor(R.color.pitchon));
			talk.setTextColor(getResources().getColor(R.color.text_nomal));
			action.setTextColor(getResources().getColor(R.color.text_nomal));
			break;
		case 1:
			picture_book.setTextColor(getResources().getColor(
					R.color.text_nomal));
			talk.setTextColor(getResources().getColor(R.color.pitchon));
			action.setTextColor(getResources().getColor(R.color.text_nomal));
			break;
		case 2:
			picture_book.setTextColor(getResources().getColor(
					R.color.text_nomal));
			talk.setTextColor(getResources().getColor(R.color.text_nomal));
			action.setTextColor(getResources().getColor(R.color.pitchon));
			break;
		default:
			break;
		}
	}

	@Override
	protected void onRestart() {
		super.onRestart();

		if (MusicService.player != null && MyApplication.playstatus == 1) {
			play.setBackgroundResource(R.drawable.player_hesitation);
			handler.post(runnable);
		} else if (MusicService.player != null && MyApplication.playstatus == 0) {
			progress.setMax(MusicService.player.getDuration());
			progress.setProgress(MusicService.player.getCurrentPosition());
		}

		isLogin = LoginUtil.isLogin(MainActivity.this);
		if (isLogin == true) {
			// 已经登录了，把名字等信息加上
			name.setText("Miky");
			sex.setVisibility(View.VISIBLE);
			age.setVisibility(View.VISIBLE);
			address.setVisibility(View.VISIBLE);
		} else {
			name.setText("点击登录");
			sex.setVisibility(View.GONE);
			age.setVisibility(View.GONE);
			address.setVisibility(View.GONE);
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK
				&& event.getAction() == KeyEvent.ACTION_DOWN) {
			if (System.currentTimeMillis() - exitTime > 2000) {
				Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
				exitTime = System.currentTimeMillis();
			} else {
				finish();
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onDestroy() {
		Intent intent = new Intent(MainActivity.this, MusicService.class);
		stopService(intent);
		MyApplication.isfirst = true;
		MyApplication.playstatus = 0;
		MyApplication.which = 0;
		MyApplication.isLoop = false;
		handler.removeCallbacks(runnable);
		super.onDestroy();
	}

}
